package com.tianyang.yukuaifu.business.main.view;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import androidx.annotation.Nullable;

import com.tianyang.yukuaifu.R;
import com.tianyang.yukuaifu.base.BaseTitleActivity;
import com.tianyang.yukuaifu.business.main.contract.BindCodePlateContract;
import com.tianyang.yukuaifu.business.main.contract.MainConfig;
import com.tianyang.yukuaifu.business.main.presenter.BindCodePlatePresenter;
import com.tianyang.yukuaifu.business.main.provider.MainOpenProvider;
import com.tianyang.yukuaifu.common.interfaces.CommonConfig;
import com.tianyang.yukuaifu.utils.ToastUtil;
import com.tianyang.yukuaifu.utils.VerifyUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Description：绑定码牌
 * Created by xyj on 2024/3/21
 */
public class BindCodePlateActivity extends BaseTitleActivity implements BindCodePlateContract.View {

    @BindView(R.id.codeTv)
    EditText codeTv;

    private BindCodePlateContract.Presenter mPresenter;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_bind_code_plate;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("绑定码牌");
        String deviceType = getIntent().getStringExtra(MainConfig.INTENT_DEVICE_TYPE);
        String title = getIntent().getStringExtra(MainConfig.INTENT_TITLE);
        if (!TextUtils.isEmpty(title)) {
            commonTitle.setTitle(title);
        }
        mPresenter = new BindCodePlatePresenter(this, deviceType);
    }

    @OnClick({R.id.captureIv, R.id.confirmTv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.captureIv:
                MainOpenProvider.getInstance().openCaptureActivity(getContext(), CommonConfig.REQUESTCODE_BIND_CODE_CAPTURE);
                break;
            case R.id.confirmTv:
                String code = codeTv.getText().toString();
                if (TextUtils.isEmpty(code)) {
                    ToastUtil.show("请点击右侧扫码绑定码牌");
                    return;
                }
                mPresenter.confirmPosBind(code);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && data != null) {
            switch (requestCode) {
                case CommonConfig.REQUESTCODE_BIND_CODE_CAPTURE:
                    String result = data.getStringExtra(MainConfig.INTENT_CAPTURE_CODE);
                    if (result != null && result.contains("sn=")) {
                        String code = result.substring(result.lastIndexOf("sn=") + 3);
                        codeTv.setText(VerifyUtil.checkString(code));
                    }
                    break;
            }
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(BindCodePlateContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
     }
}
